%% Author: john
%% Created: 2010-6-7
%% Description: TODO: Add description to mymsg
-module(mymsg).

%%
%% Include files
%%

%%
%% Exported Functions
%% How to use
%% Pid=mymsg:start().
%% mymsg:area(Pid,{hello}).
%%
-export([start/0,area/2]).

%%
%% API Functions
%%
start()->
	spawn(fun loop/0).

area(Pid,What)->
	rpc(Pid,What).
	
%%
%% Local Functions
%%

rpc(Pid,Req)->
	Pid!{self(),Req},
	io:format("rpc():~w~w~n",[self(),Req]),
    receive
		{Pid,Res}->
			io:format("rpc()-receive:~w~w~n",[Pid,Req]),
			rpc(Pid,Req),
			Res
    end.

loop()->
	receive
		{From,{a,b}} ->
			From!{self(),1},
			loop();
		{From,{hello}}->
			io:format("loop()-hello:~w~w~n",[From,{hello}]),
			From ! {self(),2},
			loop();
		{From,Other}->
			From!{self(),{error,Other}},
			loop()
	end.